import 'package:flutter/material.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';
import 'package:my_app/base/net/jd_request.dart';
import 'package:my_app/base/utils/jd_log.dart';
import 'package:my_app/base/widget/basic/base_lifecycle_page.dart';
import 'package:my_app/base/widget/basic/jd_app_bar.dart';
import 'package:my_app/builder/test_model.dart';
import 'package:my_app/builder/test_model.g.dart';
import 'package:my_app/modules/pages/home/widgets/dashed_circular_progress_bar.dart';
import 'package:my_app/modules/pages/home/widgets/draw_score_widget.dart';
import 'package:my_app/modules/router/router_contract_page.dart';
import 'package:my_app/modules/widgets/jd_button.dart';
import 'package:my_app/modules/widgets/jd_scroll_view.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends BaseLifecycleState<HomePage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    test();
    testRequest();
  }

  testRequest() async {
    JDApiRequest.get('money.json').request.then((value) {
      jdLog('value---${value}');
    });
  }

  test() {
    TestModel testModel = TestModel('江东', 18);
    Map<String, dynamic> map = testModel.toJson();
    jdLog('map---$map');
    TestModel newModel = TestModelJsonExtension.fromJson(map);
    jdLog('newModel---${newModel.name}  ${newModel.age}');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: JDAppBar(
        title: '首页',
        showBackButton: false,
        actionWidgets: [
          JDButton(
            minHeight: 30,
            minWidth: 40,
            radius: 10,
            onPressed: () {
              RouterContractPage.instance.detailPage
                  .push(context, params: {'id': '123'});
            },
            text: '点击',
          )
        ],
      ),
      body: (JDScrollView(
        children: [
          SizedBox(
            width: 300,
            height: 300,
            child: DrawScoreWidget(),
          ),
          20.vGap,
          Container(
            width: 150,
            height: 150,
            color: Colors.green.withOpacity(0.5),
            alignment: Alignment.center,
            child: getScoreWidget(90),
          ),
          30.vGap,
          InkWell(
            child: const Text(' 数据库demo 点击'),
            onTap: () {
              RouterContractPage.instance.dBDemoPage.push(context);
            },
          )
        ],
      )),
    );
  }

  final ValueNotifier<double> _valueNotifier = ValueNotifier(0);

  getScoreWidget(double theProgress) {
    if (theProgress < 15) {
      _valueNotifier.value = theProgress;
    }
    return DashedCircularProgressBar.square(
      dimensions: 95,
      valueNotifier: _valueNotifier,
      progress: theProgress,
      maxProgress: 100,
      startAngle: 180 + 60,
      sweepAngle: 360 - 120 + 20,
      foregroundColor: const Color(0xFFB4FAFF),
      backgroundColor: Colors.white.withOpacity(0.3),
      foregroundStrokeWidth: 6,
      backgroundStrokeWidth: 6,
      corners: StrokeCap.round,
      foregroundGapSize: 15,
      foregroundDashSize: 1,
      backgroundGapSize: 15,
      backgroundDashSize: 1,
      animation: theProgress > 15 ? true : false,
      child: Center(
        child: ValueListenableBuilder(
            valueListenable: _valueNotifier,
            builder: (_, double value, __) => Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    5.vGap,
                    Text(
                      '${value.toInt()}',
                      style: const TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.w600,
                          fontSize: 46),
                    ),
                  ],
                )),
      ),
    );
  }
}
